package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbDemolitionPhaseDo;
import com.yxx.business.entity.TbDemolitionPhaseVo;
import com.yxx.business.entity.TbDemolitionPhaseQueryDto;
import com.yxx.business.entity.TbDemolitionPhaseEditDto;
import com.yxx.business.service.ITbDemolitionPhaseService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 拆除处置阶段Controller
 *
 * @author yxx
 * @date 2025-08-16
 */
@Tag(name = "拆除处置阶段管理-TbDemolitionPhase")
@RestController
@RequestMapping("/business/demolitionPhase")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbDemolitionPhaseController extends BaseControllerPlus {

    private final ITbDemolitionPhaseService tbDemolitionPhaseService;

    @Operation(summary = "查询--拆除处置阶段列表")
    @GetMapping("/list")
    public PageResult<TbDemolitionPhaseVo> list(@ParameterObject TbDemolitionPhaseQueryDto tbDemolitionPhaseQueryDto) {
        return tbDemolitionPhaseService.selectTbDemolitionPhaseVoPage(tbDemolitionPhaseQueryDto);
    }

    @Operation(summary = "查询--拆除处置阶段单个")
    @GetMapping(value = "/{demolitionId}")
    public R<TbDemolitionPhaseVo> getInfo(@PathVariable Long demolitionId) {
        return R.ok(tbDemolitionPhaseService.selectTbDemolitionPhaseVoByDemolitionId(demolitionId));
    }

    @Operation(summary = "新增--拆除处置阶段")
    @Log(title = "拆除处置阶段", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody @Validated TbDemolitionPhaseEditDto tbDemolitionPhaseQueryDto) {
        return toResult(tbDemolitionPhaseService.insertTbDemolitionPhase(tbDemolitionPhaseQueryDto));
    }

    @Operation(summary = "修改--拆除处置阶段")
    @Log(title = "拆除处置阶段", businessType = BusinessType.UPDATE)
    @PutMapping("/{demolitionId}")
    public R<String> update(@PathVariable Long demolitionId, @RequestBody @Validated TbDemolitionPhaseEditDto tbDemolitionPhaseQueryDto) {
        return toResult(tbDemolitionPhaseService.updateTbDemolitionPhase(demolitionId, tbDemolitionPhaseQueryDto));
    }

    @Operation(summary = "删除--拆除处置阶段")
    @PreAuthorize("@ss.hasPermi('business:demolitionPhase:remove')")
    @Log(title = "拆除处置阶段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{demolitionIds}")
    public R<String> delete(@PathVariable List<Long> demolitionIds) {
        return toResult(tbDemolitionPhaseService.deleteTbDemolitionPhaseByDemolitionIds(demolitionIds));
    }

    @Operation(summary = "导出--拆除处置阶段列表")
    @PreAuthorize("@ss.hasPermi('business:demolitionPhase:export')")
    @Log(title = "拆除处置阶段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbDemolitionPhaseQueryDto tbDemolitionPhaseQueryDto) {
        List<TbDemolitionPhaseDo> list = tbDemolitionPhaseService.selectTbDemolitionPhaseDoList(tbDemolitionPhaseQueryDto);
        ExcelUtil<TbDemolitionPhaseDo> util = new ExcelUtil<>(TbDemolitionPhaseDo.class);
        util.exportExcel(response, list, "拆除处置阶段数据");
    }

    @Operation(summary = "导出--拆除处置阶段模板")
    @PreAuthorize("@ss.hasPermi('business:demolitionPhase:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbDemolitionPhaseDo> util = new ExcelUtil<>(TbDemolitionPhaseDo.class);
        util.importTemplateExcel(response, "导入拆除处置阶段模板数据");
    }

    @Operation(summary = "导入--拆除处置阶段列表")
    @PreAuthorize("@ss.hasPermi('business:demolitionPhase:import')")
    @Log(title = "拆除处置阶段", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbDemolitionPhaseDo> util = new ExcelUtil<>(TbDemolitionPhaseDo.class);
        List<TbDemolitionPhaseDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbDemolitionPhaseService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
